
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

package com.hlkj.pay.config;

import java.util.List;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.hlkj.pay.interceptor.AdminTokenInterceptor;
import com.hlkj.pay.interceptor.AdminUserPermissionInterceptor;

import lombok.RequiredArgsConstructor;

/**
 * @author HlpayTeam
 * @date 2024/09/10 10:51
 */
@EnableWebMvc
@Configuration
@EnableConfigurationProperties(InterceptorProperties.class)
@ConditionalOnProperty(prefix = InterceptorProperties.ADMIN_PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
@RequiredArgsConstructor
public class AdminInterceptorConfig implements WebMvcConfigurer {

    private final AdminTokenInterceptor adminTokenInterceptor;

    private final AdminUserPermissionInterceptor adminUserPermissionInterceptor;


    private final InterceptorProperties interceptorProperties;
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/" };
    /**
     * 配置拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 优先 token拦截器
        registry.addInterceptor(adminTokenInterceptor).addPathPatterns(interceptorProperties.getUrls()).excludePathPatterns(
                interceptorProperties.getIgnoreTokenUrl());
        // 最后接口对应的权限权限拦截器
        registry.addInterceptor(adminUserPermissionInterceptor).addPathPatterns(interceptorProperties.getUrls())
                .excludePathPatterns(interceptorProperties.getIgnorePermissionUrl());
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //支持webjars
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        //支持swagger
        // registry.addResourceHandler("swagger-ui.html")
        //         .addResourceLocations("classpath:/META-INF/resources/");
        // //支持小刀
        // registry.addResourceHandler("doc.html")
        //         .addResourceLocations("classpath:/META-INF/resources/");
        //支持小刀
        registry.addResourceHandler("**.html")
                .addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
    }
    // @Override
    // public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    //     converters.removeIf(converter -> converter instanceof MappingJackson2XmlHttpMessageConverter);
    // }
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 不清除默认转换器，保留其他格式的支持
        // 注册 JSON 消息转换器
        converters.add(new MappingJackson2HttpMessageConverter());
        // 可以根据需要添加其他消息转换器
        converters.add(new StringHttpMessageConverter()); // 处理 String 类型
        converters.add(new FormHttpMessageConverter());   // 处理表单数据
        converters.add(new Jaxb2RootElementHttpMessageConverter()); // 处理 XML
    }
}
